package com.zzstm.eduservice.controller;


import com.zzstm.commonUtils.R;
import com.zzstm.eduservice.entity.EduCourse;
import com.zzstm.eduservice.entity.vo.CourseInfoVO;
import com.zzstm.eduservice.entity.vo.CoursePublishVO;
import com.zzstm.eduservice.entity.vo.CourseQuery;
import com.zzstm.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-10-30
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    //查询全部课程信息
    @GetMapping("/getAllCourse")
    public R getAllCourse(){
        List<CourseInfoVO> courseInfoVOList = courseService.getCourseInfoList();
        return R.ok().data("courseInfoVOList",courseInfoVOList);
    }

    //条件查询带分页
    @PostMapping("/pageCourseCondition/{pageNum}/{pageSize}")
    public R pageTeacherCondition(@PathVariable Long pageNum, @PathVariable Long pageSize,
                                  @RequestBody(required = false) CourseQuery courseQuery){
        Map<String,Object> map = courseService.pageCourseCondition(pageNum,pageSize,courseQuery);
        return R.ok().data(map);
    }

    //添加课程信息
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVO courseInfoVO){
        String id = courseService.saveCourseInfo(courseInfoVO);
        return R.ok().data("courseId",id);
    }

    //根据课程id查询课程基本信息
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVO courseInfoVO = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVO",courseInfoVO);
    }

    //修改课程信息
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVO courseInfoVO){
        courseService.updateCourseInfo(courseInfoVO);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("/getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId){
        CoursePublishVO coursePublishVO = courseService.getPublishCourseInfo(courseId);
        return R.ok().data("coursePublishVO",coursePublishVO);
    }

    //发布课程 、只需修改状态为Normal
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.removeCourseById(courseId);
        return R.ok();
    }


}

